from mcp.server.fastmcp import FastMCP

# 创建数学计算MCP服务
math_mcp = FastMCP("Math Calculator")

@math_mcp.tool()
def add(a: float, b: float) -> float:
    """加法运算"""
    return a + b

@math_mcp.tool()
def subtract(a: float, b: float) -> float:
    """减法运算"""
    return a - b

@math_mcp.tool()
def multiply(a: float, b: float) -> float:
    """乘法运算"""
    return a * b

@math_mcp.tool()
def divide(a: float, b: float) -> float:
    """除法运算"""
    if b == 0:
        raise ValueError("除数不能为零")
    return a / b

@math_mcp.tool()
def power(base: float, exponent: float) -> float:
    """幂运算"""
    return base ** exponent

@math_mcp.resource("math://{operation}/{a}/{b}")
def math_resource(operation: str, a: float, b: float) -> str:
    """数学运算资源"""
    ops = {
        'add': add,
        'subtract': subtract,
        'multiply': multiply,
        'divide': divide,
        'power': power
    }
    if operation not in ops:
        return f"不支持的运算: {operation}"
    
    try:
        result = ops[operation](a, b)
        return f"{a} {operation} {b} = {result}"
    except Exception as e:
        return f"计算错误: {str(e)}"

if __name__ == "__main__":
    math_mcp.run(transport="stdio")